﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QQ.Observer.Q2
{
    public interface IObserver
    {
        void SendData(Stock stock);
    }

    public class Investor : IObserver
    {
        private string _name;

        public Investor(string name)
        {
            this._name = name;
        }

        public void SendData(Stock stock)
        {
            Console.WriteLine("Notified {0} of {1}'s change to {2:c}", this._name, stock.Symbol, stock.Price);
        }
    }


    public abstract class Stock
    {
        protected List<IObserver> _observers;
        private string _symbol;
        private double _price;

        public Stock(string symbol, double price)
        {
            this._symbol = symbol;
            this._price = price;
            _observers = new List<IObserver>();
        }

        public string Symbol
        {
            get { return _symbol; }
            set { _symbol = value; }
        }

        public double Price
        {
            get { return _price; }
            set { _price = value; }
        }

        public void Update()
        {
            foreach (var observer in this._observers)
            {
                observer.SendData(this);
            }

        }
        public void AddObserver(IObserver observer)
        {
            this._observers.Add(observer);

        }
        public void RemoveObserver(IObserver observer)
        {
            this._observers.Remove(observer);

        }
    }

    public class Microsoft : Stock
    {
        public Microsoft(string symbol, double price)
            : base(symbol, price)
        {
        }

    }


    public class Q2Main
    {
        public static void QMain()
        {
            var investor = new Investor("Jom");
            var investor1 = new Investor("kiko");

            var ms = new Microsoft("microsoft", 130.00);
            ms.AddObserver(investor);
            ms.AddObserver(investor1);
            ms.Update();

            ms.Symbol = "Microsoft";
            ms.Price = 120.00;
            ms.Update();

            ms.Price = 136.00;
            ms.Update();

            Console.ReadKey();

        }
    }
}
